<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/4/13
 * Time: 上午11:11
 *
 * 功能：修复优惠券统计数
 * 版本：v1.0
 *
 *
 * 规则：每天0点1分
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\activity\CouponRecord as mCouponRecord;
use Biz\model\activity\CouponConfig as mCouponConfig;


$where = [
    'coupon_id[>]' => 0
];

//取出待处理的数据
$couponConfigList = mCouponConfig::select("*", $where);

if (empty($couponConfigList)) {
    throw new \Exception("当前时刻没有待处理的优惠券数据");
}

foreach ($couponConfigList as $couponConfig) {


    //领取数

    $receivedCount = mCouponRecord::count(['coupon_id' => $couponConfig['coupon_id']]);


    //订单使用数

    $usedCount = mCouponRecord::count(['use_time[!]' => '0000-00-00 00:00:00', 'coupon_id' => $couponConfig['coupon_id']]);


    mCouponConfig::update([
        'received_count' => $receivedCount,
        'used_count'     => $usedCount
    ], ['coupon_id' => $couponConfig['coupon_id']]);


}







